//
//  CYHomePrejectTableViewCell.m
//  cyydt
//
//  Created by chu on 2019/1/16.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYHomePrejectTableViewCell.h"
#import "CYHomeProjectCollectionViewCell.h"
#import "CYProjectModel.h"
#import "CYProjectDetailViewController.h"
@interface CYHomePrejectTableViewCell ()

@property (nonatomic, strong) NSMutableArray *dataSourceArr;

@end

@implementation CYHomePrejectTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"CYHomeProjectCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CYHomeProjectCollectionViewCell"];

    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
}

- (void)setIsXiangMu:(BOOL)isXiangMu{
    _isXiangMu = isXiangMu;
    if (_isXiangMu) {
        self.biaotiLabel.text = @"项目推荐";
    }else{
        self.biaotiLabel.text = @"导师推荐";
    }
    [self.collectionView reloadData];
}

- (IBAction)moreAction:(id)sender {

    if (self.block) {
        self.block();
    }
}

- (void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    [self.dataSourceArr removeAllObjects];
    NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *dic in _dataArr) {
        CYProjectModel *model = [CYProjectModel mj_objectWithKeyValues:dic];
        NSString *htmlUrl = [NSString stringWithFormat:@"%@&type=1", [self URLDecodedString:model.mobile_url]];
        [muArr addObject:htmlUrl];
        [self.dataSourceArr addObject:model];
    }
    [self loadHtmlWithCacheWithUrls:muArr];
    [self.collectionView reloadData];
}

- (void)loadHtmlWithCacheWithUrls:(NSArray *)urls{
    
    STMURLCache *sCache = [STMURLCache create:^(STMURLCacheMk *mk) {
        mk.isUsingURLProtocol(NO).cacheTime(20*60*60);
    }];
    [sCache preLoadByWebViewWithUrls:urls];
}

-(NSString *)URLDecodedString:(NSString *)str
{
    NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    
    return decodedString;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CYProjectDetailViewController *detail = [[CYProjectDetailViewController alloc] init];
    detail.model = self.dataSourceArr[indexPath.item];
    
    [[AppDelegate sharedAppDelegate] pushViewController:detail];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CYHomeProjectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CYHomeProjectCollectionViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[CYHomeProjectCollectionViewCell alloc] init];
    }
    CYProjectModel *model = self.dataSourceArr[indexPath.item];
    if (self.isXiangMu) {
        cell.model = model;
    }else{
        cell.daoshomodel = model;
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(145, 197);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(1, 15, 15, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 15;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 15;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}

- (NSMutableArray *)dataSourceArr{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArr;
}

@end
